home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / oogl / util / porting.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-09  |  4.3 KB  |  166 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips */
  11.  
  12.  
  13. /*
  14.  * Routines for porting to brain-damaged operating systems.
  15.  * Contents:
  16.  *    strdup(str)
  17.  *    putenv(name
  18.  */
  19.  
  20. #ifdef NeXT
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. char *
  25. strdup( char *str )
  26. {
  27.     char *s;
  28.     int len;
  29.  
  30.     if(str == NULL)
  31.     return NULL;
  32.     len = strlen(str);
  33.     s = malloc(len+1);
  34.     memcpy(s, str, len+1);
  35.     return s;
  36. }
  37.  
  38.  
  39. /*
  40.  * Copyright (c) 1987 Regents of the University of California.
  41.  * All rights reserved.
  42.  *
  43.  * Redistribution and use in source and binary forms are permitted
  44.  * provided that the above copyright notice and this paragraph are
  45.  * duplicated in all such forms and that any documentation,
  46.  * advertising materials, and other materials related to such
  47.  * distribution and use acknowledge that the software was developed
  48.  * by the University of California, Berkeley.  The name of the
  49.  * University may not be used to endorse or promote products derived
  50.  * from this software without specific prior written permission.
  51.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  52.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  53.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  54.  */
  55.  
  56. #if defined(LIBC_SCCS) && !defined(lint)
  57. static char sccsid[] = "@(#)getenv.c    5.5 (Berkeley) 6/27/88";
  58. #endif /* LIBC_SCCS and not lint */
  59.  
  60. #include <stdio.h>
  61.  
  62. /*
  63.  * _findenv --
  64.  *    Returns pointer to value associated with name, if any, else NULL.
  65.  *    Sets offset to be the offset of the name/value combination in the
  66.  *    environmental array, for use by setenv(3)/putenv(3) and unsetenv(3).
  67.  *    Explicitly removes '=' in argument name.
  68.  */
  69. static char *
  70. _findenv(name, offset)
  71.     register char *name;
  72.     int *offset;
  73. {
  74.     extern char **environ;
  75.     register int len;
  76.     register char **P, *C;
  77.  
  78.     for (C = name, len = 0; *C && *C != '='; ++C, ++len);
  79.     for (P = environ; *P; ++P)
  80.         if (!strncmp(*P, name, len))
  81.             if (*(C = *P + len) == '=') {
  82.                 *offset = P - environ;
  83.                 return(++C);
  84.             }
  85.     return(NULL);
  86. }
  87. #if defined(LIBC_SCCS) && !defined(lint)
  88. /* static char sccsid[] = "@(#)setenv.c    5.2 (Berkeley) 6/27/88"; */
  89. #endif /* LIBC_SCCS and not lint */
  90.  
  91. #include <sys/types.h>
  92.  
  93. /*
  94.  * putenv --
  95.  *    Put a string of the form "name=value" into the environment.
  96.  *    [Adapted from BSD routine setenv(name, value, rewrite).]
  97.  */
  98. putenv(register char *name)
  99. {
  100.     extern char **environ;
  101.     static int alloced;            /* if allocated space before */
  102.     register char *value;
  103.     register char *C;
  104.     int l_value, offset;
  105.  
  106.     value = strchr(name, '=');
  107.     value = value ? value+1 : name;
  108.     l_value = strlen(value);
  109.     if ((C = _findenv(name, &offset))) {    /* find if already exists */
  110.         if (strlen(C) >= l_value) {    /* old larger; copy over */
  111.             while (*C++ = *value++);
  112.             return(0);
  113.         }
  114.     }
  115.     else {                    /* create new slot */
  116.         register int    cnt;
  117.         register char    **P;
  118.  
  119.         for (P = environ, cnt = 0; *P; ++P, ++cnt);
  120.         if (alloced) {            /* just increase size */
  121.             environ = (char **)realloc((char *)environ,
  122.                 (u_int)(sizeof(char *) * (cnt + 2)));
  123.             if (!environ)
  124.                 return(-1);
  125.         }
  126.         else {                /* get new space */
  127.             alloced = 1;        /* copy old entries into it */
  128.             P = (char **)malloc((u_int)(sizeof(char *) *
  129.                 (cnt + 2)));
  130.             if (!P)
  131.                 return(-1);
  132.             bcopy(environ, P, cnt * sizeof(char *));
  133.             environ = P;
  134.         }
  135.         environ[cnt + 1] = NULL;
  136.         offset = cnt;
  137.     }
  138.     for (C = name; *C && *C != '='; ++C);    /* no `=' in name */
  139.     if (!(environ[offset] =            /* name + `=' + value */
  140.         malloc((u_int)((int)(C - name) + l_value + 2))))
  141.         return(-1);
  142.     for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
  143.     for (*C++ = '='; *C++ = *value++;);
  144.     return(0);
  145. }
  146.  
  147. /*
  148.  * unputenv(name) --
  149.  *    Delete environmental variable "name".
  150.  */
  151. void
  152. unputenv(name)
  153.     char    *name;
  154. {
  155.     extern    char    **environ;
  156.     register char    **P;
  157.     int    offset;
  158.  
  159.     while (_findenv(name, &offset))        /* if set multiple times */
  160.         for (P = &environ[offset];; ++P)
  161.             if (!(*P = *(P + 1)))
  162.                 break;
  163. }
  164.  
  165. #endif /*NeXT*/
  166.